package com.admin.service.impl;

import com.admin.persistence.sys.entity.Jurisdtion;
import com.admin.persistence.sys.entity.User;
import com.admin.persistence.sys.mapper.JurisdtionMapper;
import com.admin.persistence.sys.mapper.RoleJurisdtionMapper;
import com.admin.persistence.sys.mapper.UserMapper;
import com.admin.service.SysJurisdtionService;
import com.admin.service.SysRoleService;
import com.admin.service.vo.SysJurisdtion;
import com.admin.service.vo.SysRole;
import com.admin.common.CommonUtils;
import com.admin.common.biz.ModelUseStatus;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

public class SysJurisdtionServiceImp implements SysJurisdtionService {

    JurisdtionMapper jurisdtionMapper;

    RoleJurisdtionMapper roleJurisdtionMapper;

    SysRoleService sysRoleService;

    UserMapper userMapper;

    @Override
    public List<SysJurisdtion> querySysJurisdtionByUser(String userId) {
        List<SysJurisdtion> result = new ArrayList<SysJurisdtion>();
        User user = userMapper.selectById(userId);
        List<SysRole> sysRoles = sysRoleService.queryByAccount(user.getAccount());
        if (!CollectionUtils.isEmpty(sysRoles)) {
            sysRoles.stream().map(cur -> {
                return cur.getJurisdtions();
            }).flatMap(list -> {
                return list.stream();
            }).forEach(jurisdtion -> {
                result.add(new SysJurisdtion(jurisdtion));
            });
        }
        return result;
    }

    @Override
    public List<SysJurisdtion> querySysJurisdtionByGroup(String groupId) {
        List<SysJurisdtion> results = new ArrayList<SysJurisdtion>();
        List<SysRole> sysRoles = sysRoleService.queryByUserGroup(groupId);
        sysRoles.stream().flatMap(flat -> {
            return flat.getJurisdtions().stream();
        }).forEach(em -> {
            results.add(new SysJurisdtion(em));
        });
        return results;
    }

    @Override
    public List<SysJurisdtion> querySysJurisdtionByAccount(String account) {
        List<SysJurisdtion> results = new ArrayList<SysJurisdtion>();
        List<SysRole> sysRoles = sysRoleService.queryByAccount(account);
        sysRoles.stream().flatMap(flat -> {
            return flat.getJurisdtions().stream();
        }).forEach(em -> {
            results.add(new SysJurisdtion(em));
        });
        return results;
    }

    @Override
    public int editionSysJurisdtion(SysJurisdtion sysJurisdtion) {
        return jurisdtionMapper.updateById(sysJurisdtion.getJurisdtion());
    }

    @Override
    public int removeSysJurisdtion(SysJurisdtion sysJurisdtion) {
        Jurisdtion jurisdtion = sysJurisdtion.getJurisdtion();
        jurisdtion.setStatus(ModelUseStatus.REMVOE.value);
        return jurisdtionMapper.updateById(jurisdtion);
    }

    @Override
    public int saveSysJurisdtion(SysJurisdtion sysJurisdtion) {
        Jurisdtion jurisdtion = sysJurisdtion.getJurisdtion();
        if (jurisdtionMapper.selectById(jurisdtion.getId()) != null) {
            return jurisdtionMapper.updateById(jurisdtion);
        } else {
            jurisdtion.setId(CommonUtils.createId());
            return jurisdtionMapper.insert(jurisdtion);
        }
    }

    @Override
    public SysJurisdtion createSysJurisdtion(SysJurisdtion sysJurisdtion) {
        Jurisdtion jurisdtion = sysJurisdtion.getJurisdtion();
        jurisdtion.setId(CommonUtils.createId());
        if (jurisdtionMapper.insert(jurisdtion) > 0) return sysJurisdtion;
        return null;
    }
}
